package at.ac.tuwien.ame;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.IteratorExtensions;

/**
 * Contains several utility methods for FTL.
 */
@SuppressWarnings("all")
public class FTLUtils {
  /**
   * Returns the package of a full-qualified class name.
   */
  public static String getPackage(final String str) {
    int _lastIndexOf = str.lastIndexOf(".");
    int _max = Math.max(0, _lastIndexOf);
    String _substring = str.substring(0, _max);
    return _substring;
  }
  
  /**
   * Returns the unqualified name of a full-qualified class name.
   */
  public static String getUnqualifiedName(final String str) {
    int _lastIndexOf = str.lastIndexOf(".");
    int _plus = (_lastIndexOf + 1);
    String _substring = str.substring(_plus);
    return _substring;
  }
  
  /**
   * Loads the corresponding Java {@link Class} for the given {@link EClassifier}.
   */
  public static Class<? extends Object> getClass(final EClassifier classifier) {
    try {
      Class<? extends Object> _xblockexpression = null;
      {
        String _name = classifier.getName();
        StringBuilder _stringBuilder = new StringBuilder(_name);
        final StringBuilder sb = _stringBuilder;
        EObject _eContainer = classifier.eContainer();
        EPackage container = ((EPackage) _eContainer);
        boolean _notEquals = (!Objects.equal(container, null));
        boolean _while = _notEquals;
        while (_while) {
          {
            StringBuilder _insert = sb.insert(0, ".");
            String _name_1 = container.getName();
            _insert.insert(0, _name_1);
            EPackage _eSuperPackage = container.getESuperPackage();
            container = _eSuperPackage;
          }
          boolean _notEquals_1 = (!Objects.equal(container, null));
          _while = _notEquals_1;
        }
        String _string = sb.toString();
        Class<? extends Object> _forName = Class.forName(_string);
        _xblockexpression = (_forName);
      }
      return _xblockexpression;
    } catch (Exception _e) {
      throw Exceptions.sneakyThrow(_e);
    }
  }
  
  /**
   * Loads the {@link EClass} with the given name from the provided XMI {@link Resource}.
   */
  public static EClass classForName(final Resource resource, final String element) {
    EClass _xblockexpression = null;
    {
      final String className = FTLUtils.getUnqualifiedName(element);
      TreeIterator<EObject> _allContents = resource.getAllContents();
      Iterable<EObject> _iterable = IteratorExtensions.<EObject>toIterable(_allContents);
      Iterable<EClass> _filter = Iterables.<EClass>filter(_iterable, EClass.class);
      for (final EClass clazz : _filter) {
        String _name = clazz.getName();
        boolean _endsWith = _name.endsWith(className);
        if (_endsWith) {
          return clazz;
        }
      }
      _xblockexpression = (null);
    }
    return _xblockexpression;
  }
}
